//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CoxRossRubinstein.h"
using namespace Cephei::QL::Methods::Lattices;
#include <gen/QL/StochasticProcess1D.h>
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Lattices::CCoxRossRubinstein::CCoxRossRubinstein (Cephei::QL::IStochasticProcess1D^ process, Double end, UInt64 steps, Double strike) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CStochasticProcess1D^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phCoxRossRubinstein = NULL;
#endif
        _Cprocess = safe_cast<CStochasticProcess1D^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::StochasticProcess1D>& _process = static_cast<boost::shared_ptr<QuantLib::StochasticProcess1D>&> (_Cprocess->GetShared ()); 
        QuantLib::Time _end = (QuantLib::Time)ValueHelper::Convert (end); //d
        QuantLib::Size _steps = (QuantLib::Size)ValueHelper::Convert (steps); //d
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //d
        _ppCoxRossRubinstein = new boost::shared_ptr<QuantLib::CoxRossRubinstein> (new QuantLib::CoxRossRubinstein ( _process,  _end,  _steps,  _strike ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Methods::Lattices::CCoxRossRubinstein::CCoxRossRubinstein (boost::shared_ptr<QuantLib::CoxRossRubinstein>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCoxRossRubinstein = NULL;
#endif
	_ppCoxRossRubinstein = &childNative;
    
}
Cephei::QL::Methods::Lattices::CCoxRossRubinstein::CCoxRossRubinstein (QuantLib::CoxRossRubinstein& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCoxRossRubinstein = NULL;
#endif
	_ppCoxRossRubinstein = new boost::shared_ptr<QuantLib::CoxRossRubinstein> (&childNative);
    
    _CoxRossRubinsteinOwner = owner;
    
}

Cephei::QL::Methods::Lattices::CCoxRossRubinstein::CCoxRossRubinstein (CCoxRossRubinstein^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCoxRossRubinstein = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCoxRossRubinstein = new boost::shared_ptr<QuantLib::CoxRossRubinstein> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Lattices::CCoxRossRubinstein::CCoxRossRubinstein (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCoxRossRubinstein = NULL;
#endif
	if (!t->IsSubclassOf(CCoxRossRubinstein::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Lattices::CCoxRossRubinstein::CCoxRossRubinstein (QuantLib::Handle<QuantLib::CoxRossRubinstein>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCoxRossRubinstein = &childNative;
	_ppCoxRossRubinstein = &static_cast<boost::shared_ptr<QuantLib::CoxRossRubinstein>>(childNative.currentLink());
    
    _CoxRossRubinsteinOwner = owner;
}
Cephei::QL::Methods::Lattices::CCoxRossRubinstein::CCoxRossRubinstein (QuantLib::Handle<QuantLib::CoxRossRubinstein> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCoxRossRubinstein = &childNative;
	_ppCoxRossRubinstein = &static_cast<boost::shared_ptr<QuantLib::CoxRossRubinstein>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Lattices::CCoxRossRubinstein::CCoxRossRubinstein (QuantLib::CoxRossRubinstein childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCoxRossRubinstein = NULL;
#endif
	_ppCoxRossRubinstein = new boost::shared_ptr<QuantLib::CoxRossRubinstein> (new QuantLib::CoxRossRubinstein (childNative));
    
}
#endif

Cephei::QL::Methods::Lattices::CCoxRossRubinstein::~CCoxRossRubinstein ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCoxRossRubinstein != NULL)
    {
	    delete _ppCoxRossRubinstein;
        _ppCoxRossRubinstein = NULL;
    }
}
Cephei::QL::Methods::Lattices::CCoxRossRubinstein::!CCoxRossRubinstein ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCoxRossRubinstein != NULL)
    {
	    delete _ppCoxRossRubinstein;
    }
}
QuantLib::CoxRossRubinstein& Cephei::QL::Methods::Lattices::CCoxRossRubinstein::GetReference ()
{
    if (_ppCoxRossRubinstein == NULL) throw REFNEW NativeNullException ();
	return **_ppCoxRossRubinstein;
}
boost::shared_ptr<QuantLib::CoxRossRubinstein>& Cephei::QL::Methods::Lattices::CCoxRossRubinstein::GetShared ()
{
    if (_ppCoxRossRubinstein == NULL) throw REFNEW NativeNullException ();
	return *_ppCoxRossRubinstein;
}
QuantLib::CoxRossRubinstein* Cephei::QL::Methods::Lattices::CCoxRossRubinstein::GetPointer ()
{
    if (_ppCoxRossRubinstein == NULL) throw REFNEW NativeNullException ();
	return &**_ppCoxRossRubinstein;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CoxRossRubinstein>& Cephei::QL::Methods::Lattices::CCoxRossRubinstein::GetHandle ()
{
	if (_phCoxRossRubinstein == NULL)
	{
		_phCoxRossRubinstein = new Handle<QuantLib::CoxRossRubinstein> (*_ppCoxRossRubinstein);
	}
	return *_phCoxRossRubinstein;
}
#endif
bool Cephei::QL::Methods::Lattices::CCoxRossRubinstein::HasNative () 
{
	return (_ppCoxRossRubinstein != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Lattices::ICoxRossRubinstein^ Cephei::QL::Methods::Lattices::CCoxRossRubinstein_Factory::Create (Cephei::QL::IStochasticProcess1D^ process, Double end, UInt64 steps, Double strike)
{
    return REFNEW CCoxRossRubinstein ( process,  end,  steps,  strike);
}
